<?php
class Delivery_Model_DbTable_Administradores
	extends Delivery_Model_DbTable_Abstract

{
	protected $_name = 'administradores';
	protected $_primary = 'idAdministrador';
	
	public function insert( $administrador ){
		
		return parent::insert(	
			$this->_fecha( $this->_getData( $administrador ), true, true )
		);	
	}
	
	public function update( $administrador ){
		
		return parent::update(
			$this->_fecha( $this->_getData( $administrador ), false, true) ,
			array('idAdministrador = ?' => $administrador->idAdministrador)
		
		);
	}
	
	public function delete( $administrador ){
		return parent::delete(
			array( 'idAdministrador = ?' => $administrador->idAdministrador)
		);
	}
	
	
	public function getById( $idAdministrador ){
		
		return 
			$this->_db->fetchRow(
				$this->select()->where('idAdministrador = ?', $idAdministrador)
			);
	}
	
	public function getByAccount( $email, $clave ){
		
		return 
			$this->_db->fetchRow(
				$this->select()
					->where('email = ?', $email)
					->where('clave = ?', $clave)					
			);
	}
	
	public function getByParams( $params, $offset = null, $limit = null, $order = null ){
		
		$select = $this->select();
	
		if ( $order ){
			$select->order( $order );
		}
		
		if ( $limit && !is_null($offset) ){
			$select->limit( $limit, $offset );
		}
		
		return $this->_db->fetchAssoc($select);
	}
	
	public function countByParams( $params )
	{
		
		$select = $this->select();
		
		$results = $this->_db->fetchAssoc($select);
		
		return count($results);
	}
	
	
	
	
	
	protected function _getData( $administrador )
	{
		
		$data =  array (
			'email'		=> $administrador->email,
   			'nombre'	=> $administrador->nombre,	   			
   			'clave'		=> $administrador->clave,
		    'tipo'		=> $administrador->tipo
   		);
   		
   		if ( ! empty($administrador->idNegocio)){
   			$data['idNegocio'] = $administrador->idNegocio; 
   		}
   		
   		return $data;
	}  
	
	
	
	
	
	

}